package com.czhoujunlv.drug.DBManager;

import android.content.ContentValues;
import android.content.Context;
import android.database.Cursor;
import android.database.sqlite.SQLiteDatabase;
import android.database.sqlite.SQLiteOpenHelper;
import android.util.Log;

import androidx.annotation.Nullable;

import com.czhoujunlv.drug.Bean.Dingdan;

import java.util.ArrayList;
import java.util.List;

public class Manager {
    public static SQLiteDatabase database;
    public static void initDB(@Nullable Context context){
        CreateTable goods_sqLite = new CreateTable(context,"goods.db",null,1);
        database = goods_sqLite.getWritableDatabase();
    }
    /**
     * 查找数据库中的药品
     * */
    public static List<Dingdan> queryAllDrug(){//查找药品
        Cursor cursor = database.query("buygoods",null,null,null,null,null,null);
        List<Dingdan> drugList = new ArrayList<>();

        while(cursor.moveToNext()){
            Dingdan sql_bean = new Dingdan();
            String name = cursor.getString(cursor.getColumnIndex("name"));
            String img = cursor.getString(cursor.getColumnIndex("img"));
            String des = cursor.getString(cursor.getColumnIndex("des"));
            int num = cursor.getInt(cursor.getColumnIndex("number"));
            int businessId = cursor.getInt(cursor.getColumnIndex("businessId"));
            Double salemoney = cursor.getDouble(cursor.getColumnIndex("salemoney"));
            int goodid = cursor.getInt(cursor.getColumnIndex("goodid"));
            Log.i("现有药品：",name);
            sql_bean.name = name;
            sql_bean.img = img;
            sql_bean.des = des;
            sql_bean.num = num;
            sql_bean.businessId =businessId;
            sql_bean.salemoney = salemoney;
            sql_bean.goodid = goodid;
            //Log.i("添加的城市：",sql_bean.getCity_name());
            drugList.add(sql_bean);
        }
        return drugList;
    }
    /**
     * 向数据库中添加数据
     ***/
    public static long addDrug(String name , String img , String des , int num , int businessId,Double salemoney,int goodid){
        ContentValues contentValues = new ContentValues();
        contentValues.put("name",name);
        contentValues.put("img",img);
        contentValues.put("des",des);
        contentValues.put("number",num);
        contentValues.put("businessId",businessId);
        contentValues.put("salemoney",salemoney);
        contentValues.put("goodid",goodid);

        return database.insert("buygoods",null,contentValues);
    }
    /**
     * 删除数据
     *
     * */
    public static int deleteDrug(){
        return database.delete("buygoods",null,null);
    }
}
